#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
#include "dulianjian.h"
#include "Timer0.h"

unsigned char KeyNum,MODE,TimeSetSelect,TimeSetFlashFlag;

void Timeshow()
{
	Read_Time();
	LCD_ShowNum(1,1,DS_1302_TIME[0],2);	
	LCD_ShowNum(1,4,DS_1302_TIME[1],2);	
	LCD_ShowNum(1,7,DS_1302_TIME[2],2);	
	LCD_ShowNum(2,1,DS_1302_TIME[3],2);	
	LCD_ShowNum(2,4,DS_1302_TIME[4],2);	
	LCD_ShowNum(2,7,DS_1302_TIME[5],2);
}
	
void TimeSet()
{
	if(KeyNum==2)
	{
		TimeSetSelect++;
		if(TimeSetSelect>5){TimeSetSelect=0;}
	}
	if(KeyNum==3)
	{
		DS_1302_TIME[TimeSetSelect]++;
		if(DS_1302_TIME[0]>100){DS_1302_TIME[0]=0;}
		if(DS_1302_TIME[1]>12){DS_1302_TIME[1]=1;}
		if(DS_1302_TIME[1]==1||DS_1302_TIME[1]==3||DS_1302_TIME[1]==5||DS_1302_TIME[1]==7||DS_1302_TIME[1]==8||DS_1302_TIME[1]==10||DS_1302_TIME[1]==12)
		{
			if(DS_1302_TIME[2]>31){DS_1302_TIME[2]=1;}
		}
		else if(DS_1302_TIME[1]==4||DS_1302_TIME[1]==9||DS_1302_TIME[1]==11)
		{
			if(DS_1302_TIME[2]>30){DS_1302_TIME[2]=1;}
		}
		else if(DS_1302_TIME[1]==2)
		{
			if(DS_1302_TIME[0]%4==0)
			{
				if(DS_1302_TIME[2]>29){DS_1302_TIME[2]=1;}
			}
			else
			{
				if(DS_1302_TIME[2]>28){DS_1302_TIME[2]=1;}
			}
		}
		if(DS_1302_TIME[0]>100){DS_1302_TIME[0]=0;}
		if(DS_1302_TIME[3]>24){DS_1302_TIME[3]=0;}
		if(DS_1302_TIME[4]>60){DS_1302_TIME[4]=0;}
		if(DS_1302_TIME[5]>60){DS_1302_TIME[5]=0;}
	}
	if(KeyNum==4)
	{
		DS_1302_TIME[TimeSetSelect]--;
		if(DS_1302_TIME[0]>100){DS_1302_TIME[0]=99;}
		if(DS_1302_TIME[1]>12){DS_1302_TIME[1]=12;}
		if(DS_1302_TIME[1]==1||DS_1302_TIME[1]==3||DS_1302_TIME[1]==5||DS_1302_TIME[1]==7||DS_1302_TIME[1]==8||DS_1302_TIME[1]==10||DS_1302_TIME[1]==12)
		{
			if(DS_1302_TIME[2]>31){DS_1302_TIME[2]=31;}
		}
		else if(DS_1302_TIME[1]==4||DS_1302_TIME[1]==9||DS_1302_TIME[1]==11)
		{
			if(DS_1302_TIME[2]>30){DS_1302_TIME[2]=30;}
		}
		else if(DS_1302_TIME[1]==2)
		{
			if(DS_1302_TIME[0]%4==0)
			{
				if(DS_1302_TIME[2]>29){DS_1302_TIME[2]=29;}
			}
			else
			{
				if(DS_1302_TIME[2]>28){DS_1302_TIME[2]=28;}
			}
		}
		if(DS_1302_TIME[0]>100){DS_1302_TIME[0]=0;}
		if(DS_1302_TIME[3]>24){DS_1302_TIME[3]=0;}
		if(DS_1302_TIME[4]>60){DS_1302_TIME[4]=0;}
		if(DS_1302_TIME[5]>60){DS_1302_TIME[5]=0;}
	}
	if(TimeSetSelect==0 && TimeSetFlashFlag==1){LCD_ShowString(1,1,"  ");}
	else{LCD_ShowNum(1,1,DS_1302_TIME[0],2);}
	if(TimeSetSelect==1 && TimeSetFlashFlag==1){LCD_ShowString(1,4,"  ");}
	else{LCD_ShowNum(1,4,DS_1302_TIME[1],2);}
	if(TimeSetSelect==2 && TimeSetFlashFlag==1){LCD_ShowString(1,7,"  ");}
	else{LCD_ShowNum(1,7,DS_1302_TIME[2],2);}
	if(TimeSetSelect==3 && TimeSetFlashFlag==1){LCD_ShowString(2,1,"  ");}
	else{LCD_ShowNum(2,1,DS_1302_TIME[3],2);}
	if(TimeSetSelect==4 && TimeSetFlashFlag==1){LCD_ShowString(2,4,"  ");}
	else{LCD_ShowNum(2,4,DS_1302_TIME[4],2);}
	if(TimeSetSelect==5 && TimeSetFlashFlag==1){LCD_ShowString(2,7,"  ");}
	else{LCD_ShowNum(2,7,DS_1302_TIME[5],2);}
}
	
void main()
{
	LCD_Init();
	DS1302_Init();
	LCD_ShowString(1,3,"-  -");
	LCD_ShowString(2,3,":  :");
	Set_Time();
	Timer0Init();
	while(1)
	{
		KeyNum=dulianjian();
		if(KeyNum==1)
		{
			if(MODE==0){MODE=1;}
			else if(MODE==1){MODE=0;}
		}
		switch(MODE)
		{
			case 0:
				Timeshow();
				break;
			case 1:
				TimeSet();
				break;	
		}
	}
}

void Timer0_Rountine(void) interrupt 1
{
	static unsigned int TimeCount=0;
	TL0 = 0x66;	
	TH0 = 0xFC;	
	TimeCount++;
	if(TimeCount>=500)
	{
		TimeCount=0;
		TimeSetFlashFlag=!TimeSetFlashFlag;
	}
}